﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppData;
using System.Data;
using System.Data.SqlClient;
using AppFormat;

namespace BFOAuction.DAL.Config
{
    public class DAL_Config
    {
        public AppFormat.MyError LoadInfo(ref DTO.Config.Config objConfig)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                
                DataTable dt = objCon.getTableProcedure("SYS_Config_SEL");
                if (dt != null && dt.Rows.Count > 0)
                {
                    objConfig = new DTO.Config.Config();
                    objConfig.s_ID = FormatData.IsNull(dt.Rows[0]["s_ID"].ToString(), "").ToString();
                    objConfig.CostCreateAuction =Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_CostCreateAuction"], 0));
                    objConfig.Cost = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_Cost"], 0));
                    objConfig.CostChange = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_CostChange"], 0));
                    objConfig.CostMultiTypeProduct = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_CostMultiTypeProduct"], 0));
                    objConfig.IncreaseValue = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_IncreaseValue"], 0));
                    objConfig.MaxCostChange = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_MaxCostChange"], 0));
                    objConfig.MinCostChange = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_MinCostChange"], 0));
                    objConfig.MinIncreaseValue = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_MinIncreaseValue"], 0));
                    objConfig.MaxIncreaseValue = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_MaxIncreaseValue"], 0));                    
                }
            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi lấy thông tin cấu hình ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Insert(DTO.Config.Config objConfig)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[9];
                p[0] = new SqlParameter("@" + DTO.Config.Config.colIncreaseValue, objConfig.IncreaseValue);
                p[1] = new SqlParameter("@" + DTO.Config.Config.colCost, objConfig.Cost);
                p[2] = new SqlParameter("@" + DTO.Config.Config.colCostChange, objConfig.CostChange);
                p[3] = new SqlParameter("@" + DTO.Config.Config.colMinIncreaseValue, objConfig.MinIncreaseValue);
                p[4] = new SqlParameter("@" + DTO.Config.Config.colMaxIncreaseValue, objConfig.MaxIncreaseValue);
                p[5] = new SqlParameter("@" + DTO.Config.Config.colMinCostChange, objConfig.MinCostChange);
                p[6] = new SqlParameter("@" + DTO.Config.Config.colMaxCostChange, objConfig.MaxCostChange);
                p[7] = new SqlParameter("@" + DTO.Config.Config.colCostMultiTypeProduct, objConfig.CostMultiTypeProduct);
                p[8] = new SqlParameter("@" + DTO.Config.Config.colCostCreateAuction, objConfig.CostCreateAuction);


                objCon.execProc("SYS_Config_ADD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi thêm thông tin cấu hình ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Update(DTO.Config.Config objConfig)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[10];
                p[0] = new SqlParameter("@" + DTO.Config.Config.colIncreaseValue, objConfig.IncreaseValue);
                p[1] = new SqlParameter("@" + DTO.Config.Config.colCost, objConfig.Cost);
                p[2] = new SqlParameter("@" + DTO.Config.Config.colCostChange, objConfig.CostChange);
                p[3] = new SqlParameter("@" + DTO.Config.Config.colMinIncreaseValue, objConfig.MinIncreaseValue);
                p[4] = new SqlParameter("@" + DTO.Config.Config.colMaxIncreaseValue, objConfig.MaxIncreaseValue);
                p[5] = new SqlParameter("@" + DTO.Config.Config.colMinCostChange, objConfig.MinCostChange);
                p[6] = new SqlParameter("@" + DTO.Config.Config.colMaxCostChange, objConfig.MaxCostChange);
                p[7] = new SqlParameter("@" + DTO.Config.Config.colCostMultiTypeProduct, objConfig.CostMultiTypeProduct);
                p[8] = new SqlParameter("@" + DTO.Config.Config.colCostCreateAuction, objConfig.CostCreateAuction);
                p[9] = new SqlParameter("@" + DTO.Config.Config.cols_ID, objConfig.s_ID);

                objCon.execProc("CA_ProductType_UPD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi cập nhật thông tin cấu hình ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }
    }
}
